'use strict';

const {Controller} = require('egg');
const ResponseUtil = require('../utils/ResponseUtil');
const RCEM = require('../utils/ResultCodeEnumMsg');

class SysUserController extends Controller {


    async getSysUserDetail() {
        let loginUserInfo = this.ctx.getLoginInfo();
        let operatorType = loginUserInfo.operatorType;
        let operatorId = loginUserInfo.operatorId;
        let createBy = loginUserInfo.createBy;
        if (!loginUserInfo || !operatorType || !operatorId) {
            let res = ResponseUtil.resError(RCEM.NO_PERMISSION[1], RCEM.NO_PERMISSION[0]);
            return this.ctx.body = ResponseUtil.success(res);
        }

        const user = await this.ctx.service.sysuser.selectUserById(operatorId);
        let resp = {
            user: null,
            roles: [],
            permissions: []
        };
        if (!user) {
            let res = ResponseUtil.resError(RCEM.NO_PERMISSION[1], RCEM.NO_PERMISSION[0]);
            return this.ctx.body = ResponseUtil.success(res);
        }
        delete user.password;
        resp.user = user;

        const roles = await this.ctx.service.permission.getRolePermission(user);
        resp.roles = roles;

        const permissions = await this.ctx.service.permission.getMenuPermission(user);
        resp.permissions = permissions;

        return this.ctx.body = ResponseUtil.success(resp);
    }


    async login() {
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.sysuser.login(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async logout(){
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.sysuser.logout(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async updatePwd() {
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.sysuser.updatePwd(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getMenus() {
        const resp = await this.ctx.service.sysmenu.getMenus();

        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getUserList() {
        let query = this.ctx.query;
        query.delFlag = 0;
        const resp = await this.ctx.service.sysuser.selectUserList(query);
        return this.ctx.body = ResponseUtil.success(resp.list, resp.total);
    }

    async getUserDetail() {
        let userId = this.ctx.params.userId;
        if (!userId) {
            return ResponseUtil.retArgErr();
        }
        const user = await this.ctx.service.sysuser.selectUserById(userId);


        if (user != null) {
           /* let isAdmin = this.ctx.service.permission.isAdmin(user.roles);
            if (isAdmin) {
                const roles = await this.ctx.service.sysrole.getRoleListAll({status: 0, delFlag: 0});
                user.roles = roles;
            }*/
        }
        return this.ctx.body = ResponseUtil.success(user);
    }

    async addUser() {
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.sysuser.insertUser(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async updateUser() {
        let body = this.ctx.request.body;
        if (!body.userId) {
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.sysuser.updateUser(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async deleteUser() {
        let userId = this.ctx.params.userId;
        if (!userId) {
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.sysuser.deleteUserById(userId);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async resetPwd() {
        let {password, userId} = this.ctx.request.body;
        if (!password || !userId) {
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.sysuser.resetPwd({userId, password});
        return this.ctx.body = ResponseUtil.success(resp);
    }


    async updateDept(){
        const resp = await this.ctx.service.sysuser.updateDept();
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async moveUser(){
        const resp = await this.ctx.service.sysuser.moveUser();
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async relevantUserToDept(){
        const resp = await this.ctx.service.sysuser.relevantUserToDept();
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async updateUserProfile(){
        const resp = await this.ctx.service.sysuser.updateUserProfile();
        return this.ctx.body = ResponseUtil.success(resp);
    }

}

module.exports = SysUserController;
